package com.bigcat.gulimall.demo.mongo.controller;


import com.bigcat.gulimall.demo.mongo.entity.Member;
import com.bigcat.gulimall.demo.mongo.service.MemberService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * 控制层
 *
 * @author sufer_yuan
 * @since 2021-09-24 16:38:39
 */
@Validated
@RestController
@RequestMapping("/member")
@AllArgsConstructor
public class MemberController {

	@Autowired
	private MemberService memberService;


	/**
	 * 获取列表
	 */
	@GetMapping("/list")
	public List<Member> list() {
		return memberService.findAll();
	}

	/**
	 * 获取
	 */
	@GetMapping("/get")
	public Member get(String id) {
//		Member member = mongoTemplate.findById(id, Member.class);
		return memberService.findById(id);

	}

	/**
	 * 添加
	 */
	@PostMapping("/add")
	public void add(@RequestBody Member member) {
		memberService.save(member);
	}


	/**
	 * 修改
	 */
	@PostMapping("/update")
	public void update(@RequestBody Member member) {
		memberService.save(member);
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	public void delete(String id) {
		memberService.deleteById(id);
	}

	/**
	 * 采用Mongotemplate方式，获得所有会员信息
	 */
	@PostMapping("/templateList")
	public List<Member> templateList() {
		List<Member> members = memberService.templateList();
		return members;
	}

	/**
	 * 分页查询
	 */
	@PostMapping("/pageList")
	public List<Member> pageList(@NotNull Integer pageNum, @NotNull Integer pageSize) {
		List<Member> members = memberService.pageList(pageNum,pageSize);
		return members;
	}

	/**
	 * 聚合查询
	 * @return
	 */
	@PostMapping("aggQuery")
	public List<Object> aggQuery(){
		return memberService.aggQuery();
	}

}

